// Copyright 2007 Google Inc. All Rights Reserved.

package com.google.jessewilson.codesearch.api;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.jessewilson.codesearch.SearchProviders;
import com.google.jessewilson.codesearch.api.spi.SearchProviderBackend;
import com.google.jessewilson.codesearch.api.spi.SearchProviderConfigurationEditor;
import com.google.jessewilson.codesearch.api.spi.SearchProviderParametersEditor;

import java.util.List;

/**
 * @author Ted Mao (tmao@google.com)
 */
public class SearchProviderFactory {

  private final Injector injector;
  private final SearchProviders searchProviders;

  @Inject public SearchProviderFactory(Injector injector, SearchProviders searchProviders) {
    this.injector = injector;
    this.searchProviders = searchProviders;
  }

  /**
   * Returns the search provider that can fulfill this query.
   */
  public SearchProviderBackend getSearchProvider(
      SearchProviderAware aware) {
    return injector.getInstance(aware.getSearchProvider().getBackend());
  }

  public SearchProviderParametersEditor getEditor(
      SearchProviderAware aware) {
    return injector.getInstance(aware.getSearchProvider().getParametersEditor());
  }

  public SearchProviderConfigurationEditor getConfigurationEditor(
      SearchProviderAware aware) {
    return injector.getInstance(aware.getSearchProvider().getConfigurationEditor());
  }

  public List<SearchProvider> getAllSearchProviders() {
    return searchProviders.getAll();
  }
}
